/**
 * Created by gongxun on 16/11/24.
 */

var KwxRecordOtherLayer = KwxBaseLayer.extend({
    mInputBox : null,
    Panel_center : null,
    mLastInputKey : null,
    ctor : function () {
        this._super();
        this.loadCocostudio("res/ccs/KwxRecordOther.json");
        mo.gameHelper.fixIosText(this.ccsNode);
        this.Panel_center = mo.seekWidgetByName(this.ccsNode, "Panel_center");
        this.mLastInputKey = "lastInputReplayId";

        mo.seekWidgetByName(this.ccsNode, "Button_ok").addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)) {
                // this.closeAction(this.Panel_center);
                var replayId = this.mInputBox.getString();
                //保存上次输入
                mo.fileHelper.setItem(this.mLastInputKey, replayId);

                var replaySaveKey = "Replay:" + replayId;
                if (mo.recordDetailedHelper.getIsRecordDetailed(replaySaveKey)){
                    mo.recordDetailedHelper.playReplayForId(replaySaveKey);
                    this.closeAction(this.Panel_center);
                }
                else{
                    mo.gameMsgManager.sendRePlayRequest(replaySaveKey);
                }

                cc.log("replayId:" + replayId);
            }
        }, this);

        mo.seekWidgetByName(this.ccsNode, "Button_close").addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEndMenuClose(type)) {
                this.closeAction(this.Panel_center);
            }
        }, this);

        mo.seekWidgetByName(this.ccsNode, "Button_cancel").addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEndMenuClose(type)) {
                this.closeAction(this.Panel_center);
            }
        }, this);

        //输入
        var inputP = mo.seekWidgetByName(this.ccsNode, "Image_inputBg").getPosition();
        var inputS = mo.seekWidgetByName(this.ccsNode, "Image_inputBg").getContentSize();

        inputP = cc.p(inputP.x + 5 + mo.curSize.x / 2, inputP.y + mo.curSize.y / 2);
        inputS = cc.size(inputS.width - 10, inputS.height - 30);

        this.mInputBox = new cc.EditBox(inputS, new cc.Scale9Sprite("res/ccs/common/Transparent.png"));
        this.mInputBox.setString("");
        this.mInputBox.setPosition(inputP);
        this.mInputBox.setReturnType(cc.KEYBOARD_RETURNTYPE_SEND);
        this.mInputBox.setInputMode(cc.EDITBOX_INPUT_MODE_SINGLELINE);
        this.mInputBox.setFontColor(cc.color(255, 255, 255));
        this.mInputBox.setFontSize(30);
        this.mInputBox.setPlaceHolder("");
        this.mInputBox.setPlaceholderFontColor(cc.color(255, 255, 255));
        this.mInputBox.setMaxLength(50);
        this.addChild(this.mInputBox);
    },

    onEnter : function(){
        this._super();
        mo.mKwxRecordOtherLayer = this;

        this.showAction(this.Panel_center);

        //自己上次输入的id
        if (mo.fileHelper.getItem(this.mLastInputKey) != undefined){
            this.mInputBox.setString(mo.fileHelper.getItem(this.mLastInputKey));
        }

        mo.eventHelper.add("RePlayResponse", this, function(RePlayResponse){
            mo.recordDetailedHelper.playReplayForId(RePlayResponse.ReplayId);
            this.closeAction(this.Panel_center);
        });
    },

    onExit : function(){
        mo.eventHelper.remove(this);
        mo.mKwxRecordOtherLayer = null;
        this._super();
    },

    setReplayId : function(replayId){
        this.mInputBox.setString(replayId);
    }
});

mo.mKwxRecordOtherLayer = null;